/**
 * @file useRegister
 * @author  fanxl
 * @description 跟视图层onMounted,onUnmounted绑定来注册和注销Field
 * @deprecated！！！ @2022.11.23
 */
import {type ComputedRef, unref, onMounted, onUnmounted, computed} from 'vue';
import {useFormInject} from './useForm';
import {useRegistOption} from './useRegistOption';
import type {LikeFieldType} from '../interface/field';

export const useRegister = (fieldProps: ComputedRef<LikeFieldType> | LikeFieldType) => {
    const form = useFormInject();
    const registerOption = useRegistOption(fieldProps, form.values);
    const fieldName = computed(() => unref(fieldProps).path);

    onMounted(() => {
        if (unref(fieldName)) {
            form.registerField(unref(fieldName), registerOption);
        }
    });

    onUnmounted(() => {
        if (unref(fieldName)) {
            form.unregisterField(unref(fieldName));
        }
    });
};
